<!DOCTYPE html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const { directLine, store } = testHelpers.createDirectLineEmulator();

        WebChat.renderWebChat(
          {
            directLine,
            store
          },
          document.getElementById('webchat')
        );

        await pageConditions.uiConnected();

        // WHEN: Reconnecting.

        const { resolve } = directLine.emulateReconnect();

        // THEN: Connectivity status should show "Network interruption occurred. Reconnecting…"

        await pageConditions.connectivityStatusShown('Network interruption occurred. Reconnecting…');

        // ---

        // WHEN: Reconnected.

        resolve();

        // THEN: Connectivity status should be hidden as it is connected.

        await pageConditions.became(
          'connectivity status is "connected"',
          // Connected means the element is not present.
          () => !pageElements.connectivityStatus(),
          1000
        );

        // ---

        // WHEN: Send a message.

        const { resolveAll } = await directLine.actPostActivity(() =>
          pageObjects.sendMessageViaSendBox('echo Hello, World!', { waitForSend: false })
        );

        await resolveAll();

        // THEN: Should send successfully.

        await pageConditions.allOutgoingActivitiesSent();

        // THEN: Should show one message.

        await pageConditions.numActivitiesShown(1);

        // ---

        // WHEN: Bot send a message.

        await directLine.emulateIncomingActivity({ text: 'Aloha!', type: 'message' });

        // THEN: Should show 2 messages.

        await pageConditions.numActivitiesShown(2);
      });
    </script>
  </body>
</html>
